package com.zpz.framework.zpzoauth.common.utils;

import com.alibaba.fastjson.JSON;
import com.zpz.framework.zpzoauth.common.result.ZpzOauthResultMsg;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;

import java.io.IOException;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.Map;
/**
 * @author zhangpanzhi
 * @time 2019-07-09
 * @description 未经本人允许请勿随便改动，尊重劳动
 * */
@Configuration
public class ZpzOauthMsgUtils {
    public static String SENDURL;
    @Value("${zpzOauth.msg.sendUrl}")
    public void setSendUrl(String sendUrl){
        SENDURL=sendUrl;
    }
    public static String MSGURL;
    @Value("${zpzOauth.msg.url}")
    public void setMsgUrl(String msgUrl){
        MSGURL=msgUrl;
    }
    public static String TMPLCODE;
    @Value("${zpzOauth.msg.tmplCode}")
    public void setTmplCode(String tmplCode){
        TMPLCODE=tmplCode;
    }
    public static boolean validateMsgCode(String phone,String code) {
        try {
            Map<String,String> param=new HashMap<>();
            param.put("phone",phone);
            param.put("validateContent",code);
            if (StringUtils.isNotBlank(TMPLCODE)){
                param.put("templateFlag",TMPLCODE);
            }
            String res = ZpzOauthHttpUtil.getInstance().doPostJsonString(MSGURL, null, JSON.toJSONString(param), null);
            if (StringUtils.isNotBlank(res)){
                Map<String,Object> re=(Map<String,Object>)JSON.parse(res);
                Object msgCode = re.get("code");
                if (msgCode!=null&&Integer.parseInt(msgCode.toString())==0){
                    return true;
                }
            }
            System.out.println(res);
        } catch (URISyntaxException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return false;

    }
    public static ZpzOauthResultMsg<Boolean> validateMsgCodeRe(String phone, String code) {
        ZpzOauthResultMsg<Boolean> error=ZpzOauthResultMsg.error();
        try {
            Map<String,String> param=new HashMap<>();
            param.put("phone",phone);
            param.put("validateContent",code);
            if (StringUtils.isNotBlank(TMPLCODE)){
                param.put("templateFlag",TMPLCODE);
            }
            String res = ZpzOauthHttpUtil.getInstance().doPostJsonString(MSGURL, null, JSON.toJSONString(param), null);
            if (StringUtils.isNotBlank(res)){
                Map<String,Object> re=(Map<String,Object>)JSON.parse(res);
                Object msgCode = re.get("code");
                if (msgCode!=null&&Integer.parseInt(msgCode.toString())==0){
                    return ZpzOauthResultMsg.okForData(true);
                } else {
                    error.setMessage(re.get("message")!=null?re.get("message").toString():"");
                    return error;
                }
            }
            System.out.println(res);
        } catch (URISyntaxException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return error;

    }
    public static ZpzOauthResultMsg<Boolean> sendMsgCode(String phone) {
        ZpzOauthResultMsg<Boolean> error=ZpzOauthResultMsg.error();
        try {
            Map<String,String> param=new HashMap<>();
            param.put("phone",phone);
            param.put("templateFlag",TMPLCODE);
            String res = ZpzOauthHttpUtil.getInstance().doPostJsonString(SENDURL, null, JSON.toJSONString(param), null);
            if (StringUtils.isNotBlank(res)){
                Map<String,Object> re=(Map<String,Object>)JSON.parse(res);
                Object msgCode = re.get("code");
                if (msgCode!=null&&Integer.parseInt(msgCode.toString())==0){
                    return ZpzOauthResultMsg.okForData(true);
                } else {
                    error.setMessage(re.get("message")!=null?re.get("message").toString():"");
                    return error;
                }
            }
        }catch (Exception e) {
            e.printStackTrace();
        }
        return error;

    }
}
